home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8306 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  60 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.sprintlink.net!eskimo!scs
  3. From: scs@eskimo.com (Steve Summit)
  4. Subject: Re: system() & error detection
  5. X-Nntp-Posting-Host: eskimo.com
  6. Message-ID: <DnnwIw.AGA@eskimo.com>
  7. Sender: news@eskimo.com (News User Id)
  8. Organization: schmorganization
  9. References: <4gt24g$dba@ncar.ucar.edu> <313728B7.2112A121@eiffel.com>
  10. Date: Sat, 2 Mar 1996 22:44:56 GMT
  11.  
  12. In article <313728B7.2112A121@eiffel.com>, "Guus Leeuw jr."
  13. <guusl@eiffel.com> writes:
  14. > James Adams wrote:
  15. >>         I am attempting to use the system() call in order to have a
  16. >> user-specified command execute in my program.
  17. >>         I am wondering how I can check whether or not the command has
  18. >> completed successfully ? ... I'd like to be
  19. >> able to do something like the following:
  20. >>         result = system(command);
  21. >>         if (result == ERROR)
  22. >>           exit(0);
  23. >> 
  24. >> Is there anyway to do this ?  Should I be using something other than
  25. >> system() ?
  26. > According to the standard (references are listed with FAQ 19.27),
  27. > system() will return -1 upon execution error. Any other value is the
  28. > return code of the called program.
  29.  
  30. Er, not quite.  The only thing the ANSI/ISO C standard specifies
  31. is that if you call system((char *)NULL), you'll get a nonzero
  32. result "only if a command processor is available."  As someone
  33. (probably Lawrence Kirby) was just reminding me, the return value
  34. when you pass system() an actual command string is implementation-
  35. defined.
  36.  
  37. Certainly, a high-quality implementation will arrange that
  38. system() returns the exit status of the executed command,
  39. as that's what many calling programs need (and expect).
  40. I suspect that James Adams is using MS-DOS, since what he asks
  41. for (as if it didn't work for him) is exactly what quality
  42. system() implementations already offer.  Under MS-DOS, however,
  43. system() does *not* return the exit status of the invoked
  44. command, and it turns out that it's not even the C library's
  45. fault, but rather COMMAND.COM's.  (There's a remote chance that
  46. this has been fixed by now; I last tested it under MS-DOS version
  47. 6.00.)  If you're stuck with MS-DOS, and the command you're
  48. invoking is a regular executable and not something built in to
  49. COMMAND.COM, you can get an accurate exit status by calling one
  50. of the spawn() routines instead of system().
  51.  
  52.                     Steve Summit
  53.                     scs@eskimo.com
  54. -- 
  55. The Communications Decency Act within the Telecommunications Act
  56. of 1996 (U.S.) is an annoying, threatening, abusive, indecent,
  57. and obscene piece of legislation which attempts to ban annoying,
  58. threatening, abusive, indecent, or obscene communication.
  59.